package com.jkoss.wine.warehouse.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.wine.warehouse.entity.WareInOrder;
import com.jkoss.wine.warehouse.entity.WareInventory;
import com.jkoss.wine.warehouse.entity.WareTransferOrder;
import com.jkoss.wine.warehouse.entity.WareTransferOrderDetails;
import com.jkoss.wine.warehouse.mapper.WareInventoryMapper;
import com.jkoss.wine.warehouse.mapper.WareTransferOrderDetailsMapper;
import com.jkoss.wine.warehouse.mapper.WareTransferOrderMapper;
import com.jkoss.wine.warehouse.service.IWareTransferOrderService;
import com.jkoss.wine.warehouse.vo.WareOutOrderVo;
import com.jkoss.wine.warehouse.vo.WareTransferOrderVo;

/**
 * 仓库调拨单 服务实现类
 * 
 * @Author Jason
 * @Version 1.0, 2018-11-30
 * @See
 * @Since com.jkoss.wine.warehouse.service.impl
 * @Description: TODO
 */
@Service
public class WareTransferOrderServiceImpl extends ServiceImpl<WareTransferOrderMapper, WareTransferOrder> implements IWareTransferOrderService {
	@Autowired
	private WareTransferOrderDetailsMapper wareTransferOrderDetailsMapper;
	@Autowired
	private WareInventoryMapper wareInventoryMapper;

	@Override
	public Page selectVoByPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByPage(page, wrapper));
		return page;
	}

	@Override
	public Page selectVoExcel(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoExcel(page, wrapper));
		return page;
	}

	@Override
	public WareTransferOrderVo selectVoById(String id) {
		return baseMapper.selectVoById(id);
	}

	@Override
	public boolean insertTransferOrder(WareTransferOrder inout_order, String iwid[], String owid[], String pnid[], String amut[], String remk[]) throws Exception {
		// 对订单详情表的操作
		if (!retBool(baseMapper.insert(inout_order))) {
			throw new Exception("添加商品出入库单失败");
		}
		addTransferOrderDetails(inout_order, iwid, owid, pnid, amut, remk);
		return true;
	}

	@Override
	public boolean updateTransferOrder(WareTransferOrder inout_order, String wtoodid[], String iwid[], String owid[], String pnid[], String amut[], String remk[]) throws Exception {
		// 对订单详情表的操作
		if (!retBool(baseMapper.updateById(inout_order))) {
			throw new Exception("添加商品出入库单失败");
		}

		Wrapper wrapper = Condition.create().ne("del", 1).eq("toid", inout_order.getId());
		List<WareTransferOrderDetails> details = wareTransferOrderDetailsMapper.selectList(wrapper);

		List list = new ArrayList();
		for (WareTransferOrderDetails wareTransferOrderDetails : details) {
			list.add(wareTransferOrderDetails.getId());
		}
		if (!retBool(wareTransferOrderDetailsMapper.deleteBatchIds(list))) {
			throw new Exception("调拨失败");
		}
		addTransferOrderDetails(inout_order, iwid, owid, pnid, amut, remk);
		return true;
	}

	/**
	 * 调拨添加详情信息
	 * 
	 * @param inout_order
	 * @param iwid
	 * @param owid
	 * @param pnid
	 * @param amut
	 * @param remk
	 * @return
	 * @throws Exception
	 */
	private boolean addTransferOrderDetails(WareTransferOrder inout_order, String iwid[], String owid[], String pnid[], String amut[], String remk[]) throws Exception {
		// 对库存表的操作
		List<WareTransferOrderDetails> list = new ArrayList<>();
		for (int j = 0; j < iwid.length; j++) {
			if (!CommonUtil.isBlank(amut[j])) {
				WareTransferOrderDetails wareTransferOrderDetails = new WareTransferOrderDetails();
				// 调出仓库id
				wareTransferOrderDetails.setOwid(owid[j]);
				// 调入仓库id
				wareTransferOrderDetails.setIwid(iwid[j]);
				// 商品规格id
				wareTransferOrderDetails.setPnid(pnid[j]);
				// 备注
				wareTransferOrderDetails.setRemk(remk[j]);

				// 商品数量
				wareTransferOrderDetails.setAmut(CommonUtil.int2(amut[j]));
				// 调拨单号id
				wareTransferOrderDetails.setToid(inout_order.getId());
				// 当前用户id
				wareTransferOrderDetails.setOperator(inout_order.getOperator());
				// 添加时间
				wareTransferOrderDetails.setAddTime(CommonUtil.getDate());
				// 是否删除 0-未删除 1-已删除
				wareTransferOrderDetails.setDel(0);
				list.add(wareTransferOrderDetails);
			}
		}
		if (!retBool(wareTransferOrderDetailsMapper.insertBatch(list))) {
			throw new Exception("添加调拨详情失败");
		}
		// 对库存表的操作
		for (int i = 0; i < iwid.length; i++) {
			if (!CommonUtil.isBlank(amut[i])) {
				// 调拨出库
				WareInventory outWareInventory = new WareInventory();
				outWareInventory.setPnid(pnid[i]);
				outWareInventory.setWhid(owid[i]);
				outWareInventory = wareInventoryMapper.selectOne(outWareInventory);
				if (CommonUtil.isBlank(outWareInventory)) {
					throw new Exception("调拨出库没有该商品");
				}
				outWareInventory.setAmut(outWareInventory.getAmut() - CommonUtil.int2(amut[i]));
				if (!retBool(wareInventoryMapper.updateById(outWareInventory))) {
					throw new Exception("调拨失败");
				}
				// 调拨入库
				WareInventory inWareInventory = new WareInventory();
				inWareInventory.setPnid(pnid[i]);
				inWareInventory.setWhid(iwid[i]);
				// 调拨入库，入库仓库没有商品，添加商品
				WareInventory oldWareInventory = wareInventoryMapper.selectOne(inWareInventory);
				if (!CommonUtil.isBlank(oldWareInventory)) {
					oldWareInventory.setAmut(oldWareInventory.getAmut() + CommonUtil.int2(amut[i]));
					if (!retBool(wareInventoryMapper.updateById(oldWareInventory))) {
						throw new Exception("调拨失败");
					}
				} else {
					inWareInventory.setAmut(CommonUtil.int2(amut[i]));
					// 当前用户id
					inWareInventory.setOperator(inout_order.getOperator());
					// 添加时间
					inWareInventory.setAddTime(CommonUtil.getDate());
					// 是否删除 0-未删除 1-已删除
					inWareInventory.setDel(0);
					if (!retBool(wareInventoryMapper.insert(inWareInventory))) {
						throw new Exception("调拨失败");
					}
				}
			}
		}
		return true;
	}

	@Override
	public boolean deleteTransferOrder(List<WareTransferOrder> list) throws Exception {

		for (WareTransferOrder wareTransferOrder : list) {

			if (!retBool(baseMapper.deleteById(wareTransferOrder.getId()))) {
				throw new Exception("删除调拨单失败");
			}

			Wrapper wrapper = Condition.create().ne("del", 1).eq("toid", wareTransferOrder.getId());
			List<WareTransferOrderDetails> wareTransferOrderDetails = wareTransferOrderDetailsMapper.selectList(wrapper);
			for (WareTransferOrderDetails wareTransferOrderDetails2 : wareTransferOrderDetails) {
				WareInventory ininventory = new WareInventory();
				ininventory.setPnid(wareTransferOrderDetails2.getPnid());
				ininventory.setWhid(wareTransferOrderDetails2.getIwid());
				ininventory = wareInventoryMapper.selectOne(ininventory);
				ininventory.setAmut(ininventory.getAmut() - wareTransferOrderDetails2.getAmut());
				if (!retBool(wareInventoryMapper.updateById(ininventory))) {
					throw new Exception("删除调拨单失败");
				}

				WareInventory outinventory = new WareInventory();
				outinventory.setPnid(wareTransferOrderDetails2.getPnid());
				outinventory.setWhid(wareTransferOrderDetails2.getOwid());
				outinventory = wareInventoryMapper.selectOne(outinventory);
				outinventory.setAmut(outinventory.getAmut() + wareTransferOrderDetails2.getAmut());
				if (!retBool(wareInventoryMapper.updateById(outinventory))) {
					throw new Exception("删除调拨单失败");
				}
			}
		}
		return true;
	}

}
